package com.kaiwukj.android.ufamily.mvp.ui.page.home.message;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.chauthai.swipereveallayout.ViewBinderHelper;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.domain.EaseAvatarOptions;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.utils.EaseUserUtils;
import com.hyphenate.easeui.widget.EaseImageView;
import com.hyphenate.util.DateUtils;
import com.kaiwukj.android.ufamily.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ChatListAdapter extends BaseQuickAdapter<EMConversation, BaseViewHolder> {

    private int openPos = -1;

    private final ViewBinderHelper binderHelper = new ViewBinderHelper();

    public ChatListAdapter(@Nullable List<EMConversation> data) {
        super(R.layout.item_chat_rows_history, data);
        binderHelper.setOpenOnlyOne(true);
        addChildClickViewIds(R.id.tv_delete, R.id.container_item);
    }

    public void closeSwipe() {
        if (openPos != -1) {
            binderHelper.closeLayout(String.valueOf(openPos));
        }
    }

    public ChatListAdapter() {
        this(new ArrayList<>());
    }

    @Override
    protected void convert(BaseViewHolder helper, EMConversation item) {
        SwipeRevealLayout swipeRevealLayout = helper.getView(R.id.swipe_reveal);

        binderHelper.bind(swipeRevealLayout, String.valueOf(helper.getAdapterPosition()));
        swipeRevealLayout.setSwipeListener(new SwipeRevealLayout.SwipeListener() {
            @Override
            public void onClosed(SwipeRevealLayout view) {
                if (openPos == helper.getAdapterPosition()) {
                    openPos = -1;
                }
            }

            @Override
            public void onOpened(SwipeRevealLayout view) {
                openPos = helper.getAdapterPosition();
            }

            @Override
            public void onSlide(SwipeRevealLayout view, float slideOffset) {

            }
        });

        String username = item.conversationId();
        TextView name = helper.getView(R.id.name);
        EaseImageView avatar = helper.getView(R.id.avatar);
        TextView unreadLabel = helper.getView(R.id.unread_msg_number);
        TextView message = helper.getView(R.id.message);
        TextView time = helper.getView(R.id.time);
        ImageView msg_state = helper.getView(R.id.msg_state);
        EaseUserUtils.setUserAvatar(getContext(), username, avatar);
        if (item.isGroup()) {
            EaseUserUtils.setGroupName(username, name);
        } else {
            EaseUserUtils.setUserNick(username, name);
        }
        helper.getView(R.id.mentioned).setVisibility(View.GONE);
        EaseAvatarOptions avatarOptions = EaseUI.getInstance().getAvatarOptions();
        if (avatarOptions != null && avatar instanceof EaseImageView) {
            EaseImageView avatarView = ((EaseImageView) avatar);
            if (avatarOptions.getAvatarShape() != 0)
                avatarView.setShapeType(avatarOptions.getAvatarShape());
            if (avatarOptions.getAvatarBorderWidth() != 0)
                avatarView.setBorderWidth(avatarOptions.getAvatarBorderWidth());
            if (avatarOptions.getAvatarBorderColor() != 0)
                avatarView.setBorderColor(avatarOptions.getAvatarBorderColor());
            if (avatarOptions.getAvatarRadius() != 0)
                avatarView.setRadius(avatarOptions.getAvatarRadius());
        }
        if (item.getUnreadMsgCount() > 0) {
            // show unread message count
            unreadLabel.setText(String.valueOf(item.getUnreadMsgCount()));
            unreadLabel.setVisibility(View.VISIBLE);
        } else {
            unreadLabel.setVisibility(View.INVISIBLE);
        }
        if (item.getAllMsgCount() != 0) {
            // show the content of latest message
            EMMessage lastMessage = item.getLastMessage();
            String content = null;
//            if(cvsListHelper != null){
//                content = cvsListHelper.onSetItemSecondaryText(lastMessage);
//            }
            message.setText(EaseSmileUtils.getSmiledText(getContext(), EaseCommonUtils.getMessageDigest(lastMessage, (getContext()))),
                    TextView.BufferType.SPANNABLE);
            if (content != null) {
                message.setText(content);
            }
            time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
            if (lastMessage.direct() == EMMessage.Direct.SEND && lastMessage.status() == EMMessage.Status.FAIL) {
                msg_state.setVisibility(View.VISIBLE);
            } else {
                msg_state.setVisibility(View.GONE);
            }
        }


    }
}
